package com.jz.community.modulemine.myCard.ui;

import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.jz.community.basecomm.base.BaseMvpActivity;
import com.jz.community.basecomm.mvp.BaseMvpPresenter;
import com.jz.community.basecomm.routerUtils.RouterCommonUtils;
import com.jz.community.basecomm.routerUtils.RouterIntentConstant;
import com.jz.community.basecomm.task.ITaskCallbackListener;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.basecomm.utils.SHelper;
import com.jz.community.basecomm.utils.eventbus.AppEvent;
import com.jz.community.basecomm.utils.eventbus.EventConfig;
import com.jz.community.commview.view.cardstack.RxAdapterUpDownAnimator;
import com.jz.community.commview.view.cardstack.RxCardStackView;
import com.jz.community.modulemine.R;
import com.jz.community.modulemine.R2;
import com.jz.community.modulemine.myCard.adapter.MyCardAdapter;
import com.jz.community.modulemine.myCard.info.MyCardInfo;
import com.jz.community.modulemine.myCard.task.GetMyCardListTask;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;

/**
 * Created by Tony.Fan on 2018/8/22 09:51
 */
@Route(path = RouterIntentConstant.MY_CARD)
public class MyCardActivity extends BaseMvpActivity implements RxCardStackView.ItemExpendListener {

    @BindView(R2.id.title_back_left)
    ImageView titleBackLeft;
    @BindView(R2.id.title_name)
    TextView titleName;
    @BindView(R2.id.title_right)
    TextView titleRight;
    @BindView(R2.id.title_right_iv)
    ImageView titleRightIv;
    @BindView(R2.id.title_toolbar)
    Toolbar titleToolbar;
    @BindView(R2.id.my_card_recyclerView)
    RxCardStackView myCardRecyclerView;
    @BindView(R2.id.card_mine_btn)
    TextView cardMineBtn;
    private MyCardAdapter myCardAdapter;


    @Override
    protected void initView() {
        super.initView();
        setImmersionBar(titleToolbar);
        titleName.setText("我的卡包");
        titleRight.setText("失效卡");
        SHelper.vis(titleRight);
    }

    @Override
    protected void initDatas() {
        super.initDatas();
        EventBus.getDefault().register(this);
        getMyCardInfo();
    }


    /**
     * @param message
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void EventBus(AppEvent message) {
        if (message.tag == EventConfig.REFRESH_MONEY_DETAIL) {
            getMyCardInfo();
        }
    }

    @Override
    protected void addListener() {
        super.addListener();
        titleBackLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        titleRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MyCardActivity.this, CardInvalidActivity.class));
            }
        });
        cardMineBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RouterCommonUtils.startNoParameterActivity(RouterIntentConstant.CARD_HOME);
            }
        });
    }

    @Override
    protected int getContentView() {
        return R.layout.module_mine_my_card_layout;
    }

    private void getMyCardInfo() {
        new GetMyCardListTask(this, new ITaskCallbackListener() {
            @Override
            public void doTaskComplete(Object o) {
                MyCardInfo myCardInfo = (MyCardInfo) o;
                if (!Preconditions.isNullOrEmpty(myCardInfo)) {
                    handleCardAdapter(myCardInfo);
                }
            }
        }).execute("1");
    }

    private void handleCardAdapter(MyCardInfo myCardInfo) {
        myCardRecyclerView.setItemExpendListener(this);
        myCardAdapter = new MyCardAdapter(this);
        myCardRecyclerView.setAdapter(myCardAdapter);
        myCardRecyclerView.setRxAdapterAnimator(new RxAdapterUpDownAnimator(myCardRecyclerView));
        myCardAdapter.updateData(myCardInfo.get_embedded().getContent());
        myCardAdapter.setToCardDetailResult(new MyCardAdapter.ToCardDetailResult() {
            @Override
            public void cardToResult(String cardInfo) {
                startActivityForResult(new Intent(MyCardActivity.this, CardDetailActivity.class)
                        .putExtra("cardInfo", cardInfo), 1);
            }
        });
    }

    @Override
    protected BaseMvpPresenter createPresenter() {
        return null;
    }

    @Override
    public void onItemExpend(boolean expend) {
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == RESULT_OK) {
            getMyCardInfo();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
